class TwitterPostsController < ApplicationController
  # GET /twitter_posts
  # GET /twitter_posts.xml
  def index
    @twitter_posts = TwitterPost.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @twitter_posts }
    end
  end

  # GET /twitter_posts/1
  # GET /twitter_posts/1.xml
  def show
    @twitter_post = TwitterPost.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @twitter_post }
    end
  end

  # GET /twitter_posts/new
  # GET /twitter_posts/new.xml
  def new
    @twitter_post = TwitterPost.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @twitter_post }
    end
  end

  # GET /twitter_posts/1/edit
  def edit
    @twitter_post = TwitterPost.find(params[:id])
  end

  # POST /twitter_posts
  # POST /twitter_posts.xml
  def create
    @twitter_post = TwitterPost.new(params[:twitter_post])

    respond_to do |format|
      if @twitter_post.save
        flash[:notice] = 'TwitterPost was successfully created.'
        format.html { redirect_to(@twitter_post) }
        format.xml  { render :xml => @twitter_post, :status => :created, :location => @twitter_post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @twitter_post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /twitter_posts/1
  # PUT /twitter_posts/1.xml
  def update
    @twitter_post = TwitterPost.find(params[:id])

    respond_to do |format|
      if @twitter_post.update_attributes(params[:twitter_post])
        flash[:notice] = 'TwitterPost was successfully updated.'
        format.html { redirect_to(@twitter_post) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @twitter_post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /twitter_posts/1
  # DELETE /twitter_posts/1.xml
  def destroy
    @twitter_post = TwitterPost.find(params[:id])
    @twitter_post.destroy

    respond_to do |format|
      format.html { redirect_to(twitter_posts_url) }
      format.xml  { head :ok }
    end
  end

  def post_to_twitter
    # create request
    req = TweetGen.generate
    # send request
    twitter = Net::HTTP.start('twitter.com')
    twitter.request(req)
  end
end
